log using DO_IPW_ALL , replace
/**********************/
/*Clean and set memory*/
/**********************/
clear all
set matsize 5000
set more off

/******************/
/*Install programs*/
/******************/
/*
cap ssc install xtabond2 
cap ssc install xtivreg2 
cap ssc install spmat 
cap ssc install spmack
*/
/*********************/
/*Sets base directory*/
/*********************/
global project "D:\IMF_INE_2_2021"  /* Set base directory                         */

local  repsBS=100                                                                           /* Number of bootstrap repetitions            */

/****************************/
/*Create anxillary variables*/
/****************************/

use "$project/imf_ehii_code.dta"
xtset ccode1 year
rename ehii y 



/* Identifies transitions */
gen timfprogram=.
replace timfprogram=1 if imf_all==1&l.imf_all==0
replace timfprogram=0 if imf_all==0&l.imf_all==0  

/* Defines lags of inequality */
gen lag1y=l1.y
gen lag2y=l2.y
gen lag3y=l3.y
gen lag4y=l4.y                       

/* Drop observations with no data to estimate probit */
keep if lag1y!=. 
keep if lag2y!=.
keep if lag3y!=.
keep if lag4y!=.                     

/* Creates lags of inequality */


gen ydep1=L5.y-L.y
gen ydep2=L4.y-L.y
gen ydep3=L3.y-L.y
gen ydep4=L2.y-L.y
gen ydep5=0                        

/* Creates leads of inequality */
gen ydep6=y-L.y
gen ydep7=F1.y-L.y
gen ydep8=F2.y-L.y
gen ydep9=F3.y-L.y
gen ydep10=F4.y-L.y
gen ydep11=F5.y-L.y
  gen ydep12=F6.y-L.y
  gen ydep13=F7.y-L.y
  gen ydep14=F8.y-L.y
  gen ydep15=F9.y-L.y

/* Define sample */
keep if timfprogram!=.	

/* Set seed */				 
set seed 12345                       



/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/************ DEFINE PROGRAM THAT COMPUTES THE INVERSE PROPENSITY SCORE REWEIGHTING ESTIMATOR FOR ALL YEARS ********************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
capture program tefpas_ipw, eclass

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: gen temp=timfprogram if ydep1!=. &timfprogram!=.
quietly: bysort year: egen mtemp=max(temp)
quietly: tab year if mtemp==1, gen(dumyears)

/* Estimates for year -5 */
cap: teffects ipw (ydep1) (timfprogram lag1y lag2y lag3y lag4y dumyears*, noconstant probit), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=_b[ATET: r1vs0.timfprogram]
}
else{
matrix b=(.)
}

quietly: drop dumyears* temp mtemp

/* Estimates for year -4 to -2*/
forvalues s=2(1)4{

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: gen temp=timfprogram if ydep`s'!=. &timfprogram!=.
quietly: bysort year: egen mtemp=max(temp)
quietly: tab year if mtemp==1, gen(dumyears)

cap: teffects ipw (ydep`s') (timfprogram lag1y lag2y lag3y lag4y dumyears*, noconstant probit), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.timfprogram])
}
else{
matrix b=(b, .)
}
quietly: drop dumyears* temp mtemp
}

/* Estimates for year -1 is zero by construction */
matrix b=(b, 0)

/* Estimates for year 0 to 10 */
forvalues s=6(1)15{

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: gen temp=timfprogram if ydep`s'!=. &timfprogram!=.
quietly: bysort year: egen mtemp=max(temp)
quietly: tab year if mtemp==1, gen(dumyears)

cap: teffects ipw (ydep`s') (timfprogram lag1y lag2y lag3y lag4y dumyears*, noconstant probit), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.timfprogram])
}
else{
matrix b=(b, .)
}
quietly: drop dumyears* temp mtemp
}

/* Post results in matrix b, variance computed by bootstrapping later */
ereturn post b
end



/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/************************************ ESTIMATION PROCEDURES AND STORING RESULTS ************************************************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/




/* 2. Bootstrap and save results */ 
xtset, clear
local  repsBS=100
bootstrap _b, reps(`repsBS') cluster(ccode1): tefpas_ipw
parmest, format(estimate min95 max95) saving("$project\resultsAll\impulse_ipw_alt", replace)

/* Results, 5-1 years before transition */
estimates store e0
nlcom (effect0: (_b[c1]+_b[c2]+_b[c3]+_b[c4]+_b[c5])/5), post
estimates store e1
/* Results, 1-5 years after transition */
estimates restore e0
nlcom (effect0: (_b[c6]+_b[c7]+_b[c8]+_b[c9]+_b[c10])/5), post
estimates store e2

/* Results, 6-11 years after transition */
estimates restore e0
nlcom (effect0: (_b[c11]+_b[c12]+_b[c13]+_b[c14]+_b[c15])/5), post
estimates store e3

log close

 translate DO_IPW_ALL.smcl DO_IPW_ALL.pdf




